package com.gzk.oeno.ihm.cave;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JButton;

import com.gzk.oeno.business.cave.Cave;
import com.gzk.oeno.ihm.MainOenoFrame;
import com.gzk.oeno.tech.ui.page.PageableDisplayer;
import com.gzk.utils.ihm.UIUtils;
import com.gzk.utils.misc.StringUtils;

public class CaveSelectorButton extends PageableDisplayer<Cave>{

	
	private static final Icon CAVE_ICON = UIUtils.loadSilentIcon("icons/cave_48.png");
	
	private static class SelectionAction extends AbstractAction{

		private final Cave cave;
		
		public SelectionAction(Cave cave){
			super(buildHtmlLabel(cave), CAVE_ICON);
			this.cave = cave;
		}
		
		@Override
		public void actionPerformed(ActionEvent ev) {
			MainOenoFrame.INSTANCE.addIFrame(new SingleCaveIFrame(cave), true);
		}
		
	}
	
	private static String buildHtmlLabel(Cave cave){
		StringBuilder sb = new StringBuilder();
		sb.append("<b>").append(cave.getName()).append("</b>")
			.append("<br/>")
			.append(cave.getProprietaire().getName());
		return StringUtils.toHTML(sb.toString());
	}
	

	@Override
	protected void buildDataComponent(Cave data) {
		setLayout(new BorderLayout());
		add(new JButton(new SelectionAction(data)));
	}
	
}
